
#include "../../inc/Ui/ShowFindDirectionResultForm.h"

using namespace Osp::Base;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;

int ShowFindDirectionResultForm::FormId = -1;

ShowFindDirectionResultForm::ShowFindDirectionResultForm(void)
{
	pListString = null;
}

ShowFindDirectionResultForm::~ShowFindDirectionResultForm(void)
{
}

bool
ShowFindDirectionResultForm::Initialize()
{
	Form::Construct(L"ShowFindDirectionResultForm");

	return true;
}

result
ShowFindDirectionResultForm::OnInitializing(void)
{
	result r = E_SUCCESS;

	// TODO: Add your initialization code here
	this->SetSoftkeyActionId(SOFTKEY_0, ID_SOFTKEY_OK);
	this->SetSoftkeyActionId(SOFTKEY_1, ID_SOFTKEY_CANCEL);
	this->AddSoftkeyActionListener(SOFTKEY_0, *this);
	this->AddSoftkeyActionListener(SOFTKEY_1, *this);

	pList = static_cast<List*>(GetControl(L"IDC_LIST_RESULT"));

	return r;
}

result
ShowFindDirectionResultForm::OnTerminating(void)
{
	result r = E_SUCCESS;

	// TODO: Add your termination code here

	return r;
}

void ShowFindDirectionResultForm::OnActionPerformed(const Control &source, int actionId){

	switch (actionId) {
		case ID_SOFTKEY_OK:
			OnActionOk();
			break;

		case ID_SOFTKEY_CANCEL:
			OnActionCancel();
			break;
		default:

			break;
	}
}

void ShowFindDirectionResultForm::OnActionCancel(){
	FormManager *pFormManager = null;
	FormManager::GetInstance(pFormManager);
	pFormManager->SetCurrentForm(WebForm::GetFormId());
}

void ShowFindDirectionResultForm::OnActionOk(){
	int currentIndex = pList->GetLastCheckedItemIndex();

	String *startPoint = null;
	String *endPoint = null;
	String *transport = null;
	Boolean *readResult = null;

	Boolean *isStartPoint = (Boolean*)pListArg->GetAt(4);


	if (isStartPoint->Equals(true)){
		startPoint = (String*)(pListString->GetAt(currentIndex));
		endPoint = (String*)(pListArg->GetAt(1));
	}else{
		endPoint = (String*)(pListString->GetAt(currentIndex));
		startPoint = (String*)(pListArg->GetAt(0));
	}

	transport = (String*)(pListArg->GetAt(2));
	readResult = (Boolean*)(pListArg->GetAt(3));

	IList *pListArgSend = new ArrayList();
	pListArgSend->Add(*(new String(*startPoint)));
	pListArgSend->Add(*(new String(*endPoint)));
	pListArgSend->Add(*(new String(*transport)));
	pListArgSend->Add(*(new Boolean(readResult->ToBool())));

	FormManager *pFormManager = null;
	FormManager::GetInstance(pFormManager);
	Form *form = null;
	pFormManager->GetForm(WebForm::GetFormId(), form);
	form->SendUserEvent(WebForm::REQUEST_FINDIRECTION, pListArgSend);
	pListString->RemoveAll(true);
	delete pListString;
	pListString = null;
	pListArg->RemoveAll(true);

	delete pListArg;
	pFormManager->SetCurrentForm(WebForm::GetFormId());
}

void ShowFindDirectionResultForm::ParseTextToList(String text){


}

void ShowFindDirectionResultForm::AddResultToList(IList *pArgs){
	Boolean *isStartPoint = (Boolean*)pArgs->GetAt(4);
	String *text = null;
	if (isStartPoint->Equals(true)){
		text = (String*)pArgs->GetAt(0);
	}else{
		text = (String*)pArgs->GetAt(1);
	}
	if (pListString == null){
		pListString = new ArrayList();
	}else{
		int count = pListString->GetCount();
		String *member = null;
		for (int i = 0; i < count; i++){
			member = (String*)pListString->GetAt(i);
			delete member;
		}
		pListString->RemoveAll();
	}

	pList->RemoveAllItems();
	int startIndex = 0;
	int endIndex = 0;
	String value = L"";

	result r = E_SUCCESS;
	while (true){
		startIndex = endIndex;
		r = text->IndexOf('^', startIndex, endIndex);
		if (IsFailed(r) || endIndex == -1){
			break;
		}

		text->SubString(startIndex, endIndex - startIndex, value);
		endIndex ++;
		pListString->Add(*(new String(value)));
		pList->AddItem(&value, null, null, null);
	}
	pList->SetItemChecked(0, true);
	pListArg = pArgs;

}

void ShowFindDirectionResultForm::OnUserEventReceivedN(RequestId requestId, IList *pArgs){
	switch (requestId) {
		case REQUEST_FINDDIRECTION_RESULT:
			AddResultToList(pArgs);
			break;
		default:
			break;
	}
}

